# -*- coding: utf-8 -*-
from __future__ import absolute_import

"""
对每一天取多年平均
"""
import glob
import numpy as np
import pandas as pd
import os


def averg_over_years(month_statistics_dir):
    files = []
    files.extend(sorted(glob.glob("{}\\*.check_loss.csv".format(month_statistics_dir))))
    arrs = []
    for file in files:
        arrs.append(np.loadtxt(file, skiprows=1, delimiter=',', usecols=(3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,
                                                        17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)))
    arr_3d = np.dstack(arrs) #<class 'tuple'>: (row, col, year)
    arr_cube = np.rollaxis(arr_3d, 2, 0) #<class 'tuple'>: (year, row, col)
    cloud_arr = np.zeros_like(arr_cube[0, :, :])
    cloud_arr = cloud_arr * (-1)
    for row in range(arr_cube.shape[1]):
        for col in range(arr_cube.shape[2]):
            data_pillar = arr_cube[:, row, col] #任意行列取数据柱
            data_pillar = data_pillar[data_pillar >= 0]
            if data_pillar is not None:
                averg_cloud = np.sum(data_pillar) / len(data_pillar)
                cloud_arr[row, col] = averg_cloud
    df = pd.DataFrame(cloud_arr, columns=(1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,
                                      17,18,19,20,21,22,23,24,25,26,27,28,29,30,31))
    col_1 = np.loadtxt(file, skiprows=1, delimiter=',', usecols=1)
    col_2 = np.loadtxt(file, skiprows=1, dtype=np.str, delimiter=',', usecols=2)

    df.insert(loc=0, column="邮编", value=col_1)
    df.insert(loc=0, column="县市", value=col_2)
    month = os.path.split(file)[0].split('\\')[4]
    out_file = "{}逐年平均结果.csv".format(month)
    df.to_csv(out_file, encoding='gb2312')



if __name__ == "__main__":
    averg_over_years("F:\\data\\modis_terra_cloud_mask\\20years\\Dec\\bin_cloud_mask")